package learn.hibernate.bean.po;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;


    /**
    * @ClassName: PoRole
    * @Description: 测试多对多，其中多对多的关系由role 维护
    * @author 宁少林
    * @date 2016年9月2日
    *
    */
@Entity
@Table(name = "role")
@DynamicInsert(true)
@DynamicUpdate(true)
public class PoRole implements java.io.Serializable {

	private int id;
	private PoRole trole;
	private String name;
	private String remark;
	private Integer seq;
	private List<PoResource> resourceList= new ArrayList<>(0);

	public PoRole() {
	}

	public PoRole(int id, String name) {
		this.id = id;
		this.name = name;
	}


	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PID")
	public PoRole getTrole() {
		return this.trole;
	}

	public void setTrole(PoRole trole) {
		this.trole = trole;
	}

	@Column(name = "NAME", nullable = false, length = 100)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "REMARK", length = 200)
	public String getRemark() {
		return this.remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Column(name = "SEQ")
	public Integer getSeq() {
		return this.seq;
	}

	public void setSeq(Integer seq) {
		this.seq = seq;
	}


	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "role_resource", 
	joinColumns = { @JoinColumn(name = "ROLE_ID", nullable = false, insertable=true, updatable = false) }, 
	inverseJoinColumns = { @JoinColumn(name = "RESOURCE_ID", nullable = false,insertable=true, updatable = false) })
	@Cascade({
		
		CascadeType.MERGE
		,
		CascadeType.SAVE_UPDATE
       /* CascadeType.DETACH,
        CascadeType.REFRESH,
        CascadeType.PERSIST,
        CascadeType.SAVE_UPDATE,
        CascadeType.ALL*/
	})
	public List<PoResource> getResourceList() {
		return this.resourceList;
	}

	public void setResourceList(List<PoResource> resourceList) {
		this.resourceList = resourceList;
	}

	@Override
	public String toString() {
		return "PoRole [id=" + id + ", trole=" + trole + ", name=" + name
				+ ", remark=" + remark + ", seq=" + seq + "]";
	}

	

}
